<template>
  <page>
    <template v-slot:header>
      <h2>
        <i class="el-icon-s-tools"></i>
        <span>{{ $t("system.scan_setting") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
      >
        <new-task 
          v-if="type === 'new_task'"
          @click="handleClose"
          :edit_id="edit_id"
        ></new-task>
      </el-dialog>
      <p class="tip">{{ $t("system.scan_setting_tip") }}</p>
      <ky-table
        :getData="getScanSetList"
        :search_data="search_data"
        :search_criteria="search_criteria"
        ref="table"
        :showSelect="true"
      >
        <template v-slot:table-action>
          <authorization-button
            class="kylin-item-button"
            @click="handleCreateNewTask"
            name="config:create"
          >
            {{ $t("new_task") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <expand-table v-if="scope.row.type < 3" :data="scope.row.kylinHostList === null ? [] : scope.row.kylinHostList"></expand-table>
              <expand-table v-else :data="scope.row.batchesList === null ? [] : scope.row.batchesList "></expand-table>
            </template>
          </el-table-column>

          <el-table-column :label="$t('system.task_name')" prop="name">
          </el-table-column>
          <el-table-column :label="$t('system.scan_object')" >
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">单主机</span>
              <span v-else-if="scope.row.type == 2">多主机</span>
              <span v-else-if="scope.row.type == 3">批次</span>
              <span v-else></span>
            </template>
          </el-table-column>

          <el-table-column prop="scanWeek" :label="$t('repeat')">
          </el-table-column>

          <el-table-column prop="scanTime" :label="$t('time')">
          </el-table-column>
          
          <!-- <el-table-column :label="$t('action')">
            <template slot-scope="scope">
              <authorization-button
                @click.native.prevent="handleTaskStart(scope.row)"
                type="text"
                size="medium"
                v-if="scope.row.scanStatus === false"
              >
                {{ $t("start") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleTaskSuspend(scope.row)"
                type="text"
                size="medium"
                v-else-if="scope.row.scanStatus === true"
              >
                {{ $t("suspend") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleTaskCancel(scope.row)"
                type="text"
                size="medium"
              >
                {{ $t("cancel") }}
              </authorization-button>
            </template>
          </el-table-column> -->

          <el-table-column prop="configType">
            <template slot-scope="scope">
              <el-button
                class="kylin-button"
                @click.native.prevent="handleEdit(scope.row)"
              >
                {{ $t("edit") }}
              </el-button>
              <el-button
                type="danger"
                @click.native.prevent="handleDelete(scope.row)"
              >
                {{ $t("delete") }}
              </el-button>
            </template>
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import AuthorizationDropdownItem from "@/components/AuthorizationDropdownItem";
import { getScanSetList, deleteScanSet, updateScanSet } from '@/api/scan'
import CreateOrUpdate from "../normalLibrary/form/create_or_update.vue";
import AuthorizationButton from "@/components/AuthorizationButton";
import NewTask from './form/new_task.vue';
import expandTable from './expandTable.vue'
export default {
  name: "scan_setting",
  components: {
    Page,
    kyTable,
    CreateOrUpdate,
    AuthorizationButton,
    AuthorizationDropdownItem,
    NewTask,
    expandTable,
  },
  methods: {
    getScanSetList,
    getScanSearch() {
      getScanSetList().then(res => {
        let results = res.data.data && res.data.data.results;
        let types = [];
        let scanWeek = [];
        let scanTime = [];

        results.forEach((item) => {
          if (!types.includes(item.type)) {
            let type = item.type === 1 ? "单主机" : item.type === 2 ? "多主机" : item.type === 3 ? "批次" : "无"
            
            types.push(item.type);
            this.search_data["type"].items.push({
              key: item.type,
              name: type,
            });
          }
          if (!scanWeek.includes(item.scanWeek)) {
            scanWeek.push(item.scanWeek);
            this.search_data["scanWeek"].items.push({
              key: item.scanWeek,
              name: item.scanWeek,
            });
          }
          if (!scanTime.includes(item.scanTime)) {
            scanTime.push(item.scanTime);
            this.search_data["scanTime"].items.push({
              key: item.scanTime,
              name: item.scanTime,
            });
          }
        })
      })
    },
    handleDelete(row) {
      deleteScanSet(row.id).then(res => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
          this.$refs.table.handleSearch();
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      })
    },
    handleEdit(row) {
      this.display = true;
      this.title = this.$t("updata_task");
      this.edit_id = row.id;
      this.type = "new_task";
    },
    handleCreateNewTask() {
      this.display = true;
      this.type = "new_task";
      this.title = this.$t("new_task");
    },
    handleTaskStart(row) {
      updateScanSet({id: row.id, scanStatus: true}).then(res => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      })
    },
    handleTaskSuspend(row) {
      updateScanSet({id: row.id, scanStatus: false}).then(res => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      })
    },
    handleTaskCancel(row) {
       updateScanSet({id: row.id, scanStatus: false}).then(res => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      })
    },
    handleClose(type) {
      this.disableName = false;
      this.display = false;
      this.edit_id = null;
      this.type = "";
      if (type === "success") {
        this.$refs.table.refresh();
      }
    },
  },
  data() {
    return {
      disableName: false,
      type: "",
      edit_id: "",
      d_normal: true,
      title: "",
      osName: "",
      osVersion: "",
      osArch: "",
      display: false,
      delete_content: "",
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
      },
      ids: [],
      search_data: {
        name: {
          name: "system.task_name",
          type: "text",
        },
        type: {
          name: "system.scan_object",
          type: "select",
          items: [],
          func: this.getScanSearch,
        },
        scanWeek: {
          name: "repeat",
          type: "select",
          items: [],
        },
        scanTime: {
          name: "time",
          type: "select",
          items: [],
        },
      },
    };
  },
};
</script>

<style>
</style>